package com.asset.dispoal.business.approval.service.command;

import com.asset.dispoal.business.approval.dto.BorrowerInfo;
import com.asset.dispoal.business.approval.dto.OperateItemInfo;
import com.asset.dispoal.business.approval.dto.RepaymentDetailResponse;
import com.asset.dispoal.business.approval.dto.RepaymentInfo;
import com.asset.dispoal.business.approval.service.converter.ApprovalConverter;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCaseMediation;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMediationMapper;
import com.asset.dispoal.business.casedispose.dto.CaseDisposalFileResponse;
import com.asset.dispoal.business.mediation.domain.db.entity.ReduceStagingTmp;
import com.asset.dispoal.business.mediation.domain.db.mapper.ReduceStagingTmpMapper;
import com.asset.dispoal.business.repayment.domain.db.entity.BsCaseRepayment;
import com.asset.dispoal.business.repayment.domain.db.entity.BsCaseRepaymentLogs;
import com.asset.dispoal.business.repayment.domain.db.mapper.CaseRepaymentLogsMapper;
import com.asset.dispoal.business.repayment.domain.db.mapper.CaseRepaymentMapper;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.StringUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description:
 * @author: yf
 * @date: 2022-10-10 10:32:25
 */
@Repository
public class GetApprovalListDetailCommand implements BaseCommand<String, RepaymentDetailResponse> {
    @Autowired
    private CaseRepaymentLogsMapper caseRepaymentLogsMapper;
    @Autowired
    private BsCaseMapper bsCaseMapper;
    @Autowired
    private ReduceStagingTmpMapper reduceStagingTmpMapper;
    @Autowired
    private BsCaseMediationMapper bsCaseMediationMapper;
    @Autowired
    private CaseRepaymentMapper caseRepaymentMapper;

    @Override
    public RepaymentDetailResponse execute(String repaymentLogId) {
        //1.将案件表借款人信息 操作人信息带出
        BsCaseRepaymentLogs repaymentLogs = caseRepaymentLogsMapper.queryById(repaymentLogId);
        BsCase bsCase = bsCaseMapper.queryById(repaymentLogs.getCaseId());
        BorrowerInfo borrowerInfo = ApprovalConverter.INSTANCE.caseInfoToBorrowInfo(bsCase);

        OperateItemInfo operateItemInfo;
        //2.判断此条还款时案件状态 获取对应的还款时费用
        switch (repaymentLogs.getRepaymentCaseState()) {
            case "reconciliation":
                //显示分期记录
                ReduceStagingTmp reduceStagingTmp = reduceStagingTmpMapper.queryById(repaymentLogs.getRepaymentId());
                operateItemInfo = ApprovalConverter.INSTANCE.stageInfoToOperateInfo(reduceStagingTmp);
                break;
            case "mediation":
                //调解分期信息
                BsCaseMediation bsCaseMediation = bsCaseMediationMapper.queryById(repaymentLogs.getRepaymentId());
                operateItemInfo = ApprovalConverter.INSTANCE.meditionInfoToOperateInfo(bsCaseMediation);
                operateItemInfo.setRepaymentCaseState("mediation");
                break;
            default:
                //显示还款时的信息
                BsCaseRepayment caseRepayment = caseRepaymentMapper.queryById(repaymentLogs.getRepaymentId());
                operateItemInfo = ApprovalConverter.INSTANCE.repaymentInfoToOperateInfo(caseRepayment);

                if (StringUtil.isNotBlank(caseRepayment.getFormulaId())) {
                    operateItemInfo.setFormularName(caseRepaymentLogsMapper.getFormularNameById(caseRepayment.getFormulaId()));
                }

                break;
        }
        BigDecimal total = nullValueHandler(operateItemInfo.getPrincipalAmount())
                .add(nullValueHandler(operateItemInfo.getInterestAmount()))
                .add(nullValueHandler(operateItemInfo.getLitigationFines()))
                .add(nullValueHandler(operateItemInfo.getLitigationAmount()))
                .add(nullValueHandler(operateItemInfo.getPreservationAmount()));
        operateItemInfo.setTotalAmt(total);
        AgreementCaseSateMenu caseSateMenu = AgreementCaseSateMenu.getByLabel(operateItemInfo.getRepaymentCaseState());
        if (caseSateMenu != null) {
            operateItemInfo.setRepaymentCaseState(caseSateMenu.getValue());
        }

        RepaymentInfo repaymentInfo = ApprovalConverter.INSTANCE.repaymentLogInfoToRepaymentInfo(repaymentLogs);
        repaymentInfo.setCreateUserName(caseRepaymentLogsMapper.getNickNameByUserId(repaymentInfo.getCreateUserId()));
        switch (repaymentInfo.getApprovalState()) {
            case "0":
                repaymentInfo.setApprovalState("未审核");
                break;
            case "1":
                repaymentInfo.setApprovalState("审核中");
                break;
            case "2":
                repaymentInfo.setApprovalState("通过");
                break;
            case "3":
                repaymentInfo.setApprovalState("未通过");
                break;
        }
        //文件查询
        if (StringUtil.isNotBlank(repaymentInfo.getRepaymentFileIds())) {
            List<CaseDisposalFileResponse> repaymentFileList = caseRepaymentLogsMapper.queryRepaymentFileById(repaymentInfo.getRepaymentFileIds());
            repaymentInfo.setRepaymentFileList(repaymentFileList);
        }
        return RepaymentDetailResponse.builder()
                .borrowerInfo(borrowerInfo)
                .operateItemInfo(operateItemInfo)
                .repaymentInfo(repaymentInfo)
                .build();
    }

    /**
     * Decimal Null值处理
     */
    private BigDecimal nullValueHandler(BigDecimal bigDecimal) {
        return bigDecimal == null ? BigDecimal.ZERO : bigDecimal;
    }
}
